﻿$(function () {
    $("#submit").click(function () {
        Submit();
    });

    $("#body").keypress(function (event) {
        if (event.which == '13') {
            Submit();
        }
    });

    $("#TypeID").change(function () {
        Ajax({
            url: "/MyAdams/Businesses/GetCategoriesByType",
            data: JSON.stringify({ typeId: $("#TypeID").val() }),
            success: function (data) {
                $("#CategoryID option:not(:first)").remove();
                for (var i in data) {
                    $("#CategoryID").append("<option value=\"" + data[i].ID + "\">" + data[i].Name + "</option>");
                }
            }
        });
    });
});

function Submit() {

    $("#submit").hide();
    var input = {
        ID: $("#id").val(),
        CategoryID: $("#CategoryID").val(),
        Name: $("#Name").val(),
        Street: $("#Street").val(),
        Street2: $("#Street2").val(),
        City: $("#City").val(),
        State: $("#State").val(),
        Zip: $("#Zip").val(),
        Phone: $("#Phone").val(),
        Description: $("#Description").val()
    };
    
    Ajax({
        url: "/MyAdams/Businesses/Save",
        data: JSON.stringify(input),
        success: function (data) {
            if (data.IsValid == false) {
                ShowValidationErrors(data);
            } else if (data.IsSaved == true) {
                location.href = "/MyAdams/Businesses/?msg=1";
            }
        },
        complete: function () {
            $("#submit").show();
        }
    });
}